// http://192.168.110.110:8080/api/user/register
export const BASE_URL = 'http://192.168.110.110:8080/'
export const request = (url, {
	method = 'post',
	data
} = {}) => {
	// console.log(data, 'data------')
	// (([key, value]) => {
	//     if (Array.isArray(value)) {
	//         return `${key}=${JSON.stringify(value)}`;
	//     }
	//     return `${key}=${value}`;
	// })
	let formDataStr = Object.entries(data)
		.map(([key, value]) => `${key}=${value}`)
		.join('&');
	// console.log('formDataStr: ', formDataStr);
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + url,
			method,
			data: formDataStr,
			timeout: 5000,
			header: {
				'Content-Type': 'application/x-www-form-urlencoded',
				token: uni.getStorageSync('token') ? uni.getStorageSync('token') : ''
			},
			success: (res) => {
				if (res.data.code === 1) {
					resolve(res.data);
				} else if (res.data.code === 401) {
					reject(res.data.msg);
					uni.removeStorageSync('token');
					uni.removeStorageSync('info');
					uni.redirectTo({
						url: '/subpack/login/login?msg=' + res.data.msg
					});
				} else {
					reject(res.data.msg);
					uni.showToast({
						title: res.data.msg,
						icon: "error"
					});
				}
			},
			fail: (err) => {
				console.log('err: ', err.errMsg);
				reject(err);
			}
		});
	});
};